﻿package mortal.game.view.npc
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.debug.*;
    import com.mui.controls.*;
    import flash.utils.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.task.data.*;

    public class NpcCache extends Object
    {
        public var isBySceneClick:Boolean = false;
        private var _selectedNpc:NPCPlayer;
        private var _selectedNpcMapId:int = -1;
        private var _showNpc:Dictionary;
        private var _showCopy:Dictionary;

        public function NpcCache()
        {
            this._showNpc = new Dictionary();
            this._showCopy = new Dictionary();
            return;
        }// end function

        public function get selectedNpcMapId() : int
        {
            return this._selectedNpcMapId;
        }// end function

        public function set selectedNpcMapId(param1:int) : void
        {
            this._selectedNpcMapId = param1;
            return;
        }// end function

        public function updateShows(param1:STaskShowMsg) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:TTaskShow = null;
            var _loc_5:int = 0;
            _loc_2 = param1.addIds;
            if (_loc_2 != null)
            {
                _loc_5 = 0;
                while (_loc_5 < _loc_2.length)
                {
                    
                    _loc_3 = _loc_2[_loc_5];
                    _loc_4 = TaskConfig.instance.getTaskShow(_loc_3);
                    if (_loc_4 == null)
                    {
                        Alert.show("TTasksShow表中找不到配置项, id=" + _loc_3);
                    }
                    else
                    {
                        this.parseAddList(_loc_4);
                    }
                    _loc_5++;
                }
            }
            return;
        }// end function

        private function parseAddList(param1:TTaskShow) : void
        {
            var _loc_2:Array = null;
            if (param1.hideCopyList != null && param1.hideCopyList != "")
            {
                _loc_2 = param1.hideCopyList.split("#");
                this.addIds(this._showCopy, _loc_2, false);
            }
            if (param1.showCopyList != null && param1.showCopyList != "")
            {
                _loc_2 = param1.showCopyList.split("#");
                this.addIds(this._showCopy, _loc_2, true);
            }
            if (param1.hideNpcList != null && param1.hideNpcList != "")
            {
                _loc_2 = param1.hideNpcList.split("#");
                this.addIds(this._showNpc, _loc_2, false);
            }
            if (param1.showNpcList != null && param1.showNpcList != "")
            {
                _loc_2 = param1.showNpcList.split("#");
                this.addIds(this._showNpc, _loc_2, true);
            }
            return;
        }// end function

        private function addIds(param1:Dictionary, param2:Array, param3:Boolean) : void
        {
            var _loc_5:int = 0;
            var _loc_4:int = 0;
            while (_loc_4 < param2.length)
            {
                
                _loc_5 = int(param2[_loc_4]);
                if (_loc_5 <= 0)
                {
                }
                else
                {
                    param1[_loc_5] = param3;
                }
                _loc_4++;
            }
            return;
        }// end function

        public function getShowNpc() : Dictionary
        {
            return this._showNpc;
        }// end function

        public function getShowCopy() : Dictionary
        {
            return this._showCopy;
        }// end function

        public function get selectedNpc() : NPCPlayer
        {
            return this._selectedNpc;
        }// end function

        public function set selectedNpc(param1:NPCPlayer) : void
        {
            this._selectedNpc = param1;
            return;
        }// end function

        public function getNpcTaskStatus(param1:int) : int
        {
            var _loc_6:TaskInfo = null;
            var _loc_2:* = this.getTaskListByNpcId(param1, false);
            if (_loc_2 == null || _loc_2.length == 0)
            {
                return NpcTaskStatus.HasNoTask;
            }
            var _loc_3:Boolean = false;
            var _loc_4:Boolean = false;
            var _loc_5:Boolean = false;
            for each (_loc_6 in _loc_2)
            {
                
                if (_loc_6.isComplete())
                {
                    return NpcTaskStatus.HasCompleteTask;
                }
                if (_loc_6.isCanget())
                {
                    _loc_3 = true;
                    continue;
                }
                if (_loc_6.isCanNotEnd())
                {
                    _loc_4 = true;
                    continue;
                }
                if (_loc_6.isFail())
                {
                    _loc_5 = true;
                }
            }
            if (_loc_3)
            {
                return NpcTaskStatus.HasCanGetTask;
            }
            if (_loc_4)
            {
                return NpcTaskStatus.HasCanNotCompleteTask;
            }
            if (_loc_5)
            {
                return NpcTaskStatus.HasFailTask;
            }
            return NpcTaskStatus.HasNoTask;
        }// end function

        public function getTaskListByNpcId(param1:int, param2:Boolean = true) : Array
        {
            var _loc_6:TaskInfo = null;
            var _loc_7:int = 0;
            var _loc_8:Dictionary = null;
            var _loc_9:SProcess = null;
            var _loc_10:Array = null;
            var _loc_11:int = 0;
            var _loc_3:* = Cache.instance.task.getTaskCanGet(param2);
            var _loc_4:Array = [];
            var _loc_5:int = 0;
            while (_loc_5 < _loc_3.length)
            {
                
                _loc_6 = _loc_3[_loc_5];
                if (_loc_6.stask.getNpc == param1)
                {
                    _loc_4.push(_loc_6);
                }
                _loc_5++;
            }
            _loc_3 = Cache.instance.task.taskDoing;
            _loc_5 = 0;
            while (_loc_5 < _loc_3.length)
            {
                
                _loc_6 = _loc_3[_loc_5];
                if (_loc_6.isComplete())
                {
                    if (param1 == _loc_6.stask.endNpc)
                    {
                        _loc_4.push(_loc_6);
                    }
                }
                else
                {
                    if (param1 == _loc_6.stask.endNpc)
                    {
                        if (_loc_6.stask.group == ETaskGroup._ETaskGroupGuild || _loc_6.stask.group == ETaskGroup._ETaskGroupEscort)
                        {
                            _loc_4.push(_loc_6);
                            ;
                        }
                    }
                    _loc_7 = _loc_6.curStep;
                    _loc_8 = _loc_6.stask.processMap;
                    if (_loc_8 == null || _loc_7 < 0 || _loc_8[_loc_7] == null)
                    {
                    }
                    else
                    {
                        _loc_9 = _loc_8[_loc_7][0];
                        if (_loc_9 == null || _loc_9.type != ETaskProcess._ETaskProcessDialog && _loc_9.type != ETaskProcess._ETaskProcessDeliver)
                        {
                        }
                        else
                        {
                            _loc_10 = _loc_9.contents;
                            _loc_11 = _loc_10[0];
                            if (_loc_11 != param1)
                            {
                            }
                            else
                            {
                                _loc_4.push(_loc_6);
                            }
                        }
                    }
                }
                _loc_5++;
            }
            _loc_4.sort(TaskRule.sortTask);
            return _loc_4;
        }// end function

        public function getTaskTalk(param1:TaskInfo) : String
        {
            var _loc_2:* = this.getTTaskDialog(param1);
            if (_loc_2 != null)
            {
                return _loc_2.talkStr;
            }
            return "";
        }// end function

        public function getTTaskDialog(param1:TaskInfo) : TTaskDialog
        {
            var _loc_4:int = 0;
            var _loc_5:SProcess = null;
            var _loc_2:* = param1.stask.getTalkId;
            switch(_loc_5.type)
            {
                case ETaskProcess._ETaskProcessDeliver:
                {
                    break;
                }
                case ETaskProcess._ETaskProcessDialog:
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (param1.isComplete())
            {
            }
            if (_loc_2 <= 0)
            {
                return null;
            }
            var _loc_3:* = TaskConfig.instance.getDialog(_loc_2);
            if (_loc_3 == null)
            {
                Log.error("tTaskDialog表中没有配置对话talkStr， dialogId = " + _loc_2);
            }
            return _loc_3;
        }// end function

    }
}
